﻿using classLibrary;
using Microsoft.AspNetCore.Mvc;
using WebAPI.Models;
using WebAPI.Services;
using static System.Net.Mime.MediaTypeNames;

namespace WebAPI.Controllers
{
    [Route("[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = nameof(ApiVersionInfo.Version2))]
    public class PizzaControllerV2 : ControllerBase
    {
        public PizzaControllerV2() { }


        /// <summary>
        /// 仅响应 HTTP GET 谓词，如 [HttpGet] 属性所示。返回类型为 List<Pizza> 的 ActionResult 实例。 
        /// ActionResult 类型是 ASP.NET Core 中所有操作结果的基类。
        /// 查询服务以获取所有披萨，并通过 Content-Type 的值 application/json 自动返回数据。
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<Pizza>> GetAll() =>
            PizzaService.GetAll();


        /// <summary>
        /// 获取指定披萨
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<Pizza> Get(int id)
        {
            var pizza = PizzaService.Get(id);

            if (pizza == null)
                return NotFound();

            return pizza;
        }

        /// <summary>
        /// 添加披萨
        /// </summary>
        /// <param name="pizza"></param>
        /// <returns></returns>
        [HttpPost]//添加
        public IActionResult Create(Pizza pizza)
        {
            PizzaService.Add(pizza);
            return CreatedAtAction(nameof(Get), new { id = pizza.Id }, pizza);
        }

        [HttpPut("{id}")]//修改
        public IActionResult Update(int id, Pizza pizza)
        {
            if (id != pizza.Id)
                return BadRequest();

            var existingPizza = PizzaService.Get(id);
            if (existingPizza is null)
                return NotFound();

            PizzaService.Update(pizza);

            return NoContent();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            var pizza = PizzaService.Get(id);

            if (pizza is null)
                return NotFound();

            PizzaService.Delete(id);

            return NoContent();
        }
    }

    
}
